package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.log.LogAnno;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    CheckItemService checkItemService;

    /**
     * 1、form表单提交数据的格式 （name=jack&age=18&address=shangshou） 后端用对象接收不需要加注解
     * 2、页面传的是json数据，后端使用map 或者 pojo时 需要加@RequestBody
     * 3、基本类型 & 数组 & MultipartFile 只要保持页面的参数名称和controller方法形参一致就不用加@RequestParam
     * 4、List 不管名字一不一样 必须加@RequestParam
     *
     * @return
     */

    @LogAnno("新增检查项")
    @RequestMapping("/add")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
            return new Result(true, "新增检查项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增检查项失败");
        }
    }

    @LogAnno("编辑检查项")
    @RequestMapping("/edit")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
            return new Result(true, "编辑检查项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑检查项失败");
        }
    }

    @LogAnno("查询分页检查项")
    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.findAll(queryPageBean);
    }


    @LogAnno("删除检查项")
    @RequestMapping("/deleteById")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    public Result deleteById(int id) {
        try {
            checkItemService.deleteById(id);
            return new Result(true, "删除检查项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除检查项失败");
        }
    }


    @LogAnno("回显编辑检查项")
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result findById(int id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, "回显编辑检查项成功", checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显编辑数据失败");


        }
    }

}